package pl.wroc.uni.ii.pastuszka.algorithms.evolutionary;

import java.util.Arrays;
import java.util.Comparator;

import pl.wroc.uni.ii.pastuszka.algorithms.evolutionary.utils.BinaryRandom;

public class Individual {
  public int[] chromosome;
  public float rank = 0;

  public Individual(int chromosomeLength) {
    chromosome = new int[chromosomeLength];
  }

  public Individual(Individual source) {
    chromosome = Arrays.copyOf(source.chromosome, source.chromosome.length);
    this.rank = source.rank;
  }

  public void fillWithRandomData(float[] probabilityVector) {
    for (int i = 0; i < probabilityVector.length; ++i) {
      chromosome[i] = BinaryRandom.getInt(probabilityVector[i]);
    }
  }

  public void fillWithRandomData() {
    for (int i = 0; i < chromosome.length; ++i) {
      chromosome[i] = BinaryRandom.getInt();
    }
  }

  @Override
  public String toString() {
    StringBuilder result = new StringBuilder();
    for (int i : chromosome) {
      result.append(i);
    }
    return result.toString();
  }

  private static class RankComparator implements Comparator<Individual> {
    @Override
    public int compare(Individual arg0, Individual arg1) {
      return Float.compare(arg0.rank, arg1.rank);
    }
  }

  public static final RankComparator RANK_COMPARATOR = new RankComparator();

}
